import config from '../config.js'

//请求
function request (req) {
	
	let data = req.data
	const method = req.method
	const url = req.url
	
	
	let header = {
		'Content-Type': 'application/json',
		'timestamp': Date.parse(new Date()),
		// 'token': uni.getStorageSync('user').access_token,
		'Authorization':'Bearer ' + uni.getStorageSync('user').access_token
	}
	// console.log(">>>>>>>>>>",  uni.getStorageSync('user'))
	uni.showLoading({
	    title: '请求中',
		mask:true
	})
	// console.log( 'HEADER:', header )
	return new Promise((resolve, reject) => {
		console.log('REQ ==>', url)
		console.log( data )
		uni.request({
			url: url, 
			data: data,
			header: header,
			method: method.toUpperCase(),
			timeout:1000 * 60 * 3, //超时三分钟
			success: async (res) => {
				uni.hideLoading()
				console.log("RES <==", url)
				console.log( res )
				console.log(res.statusCode == 200 , res.data.code == 200)
				if (res.statusCode == 200 && res.data.code == 200){
					resolve(res.data.data)
				}else if (res.statusCode == 444){
					//处理token验证出错
					console.log('Token过期')
				}
			},
			fail: (err) => {
				uni.hideLoading()	
				uni.showToast({
				    title: '服务器休息中，请稍后再试',
					icon: "none",
					position:"center",
				    duration: 3000
				});
				console.error(url, err)
				reject(err)
			}
		});
	})
}

export default request